//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Shp.Runtime.Contract;
using System.Data;
using LitwareHR.Portfolio.Gateways;
using System.Globalization;

namespace LitwareHR.PublicApp.Presenters.Careers.Evaluation
{
    public class ApplicantPresenter
    {
        private IApplicantView _view;

        public ApplicantPresenter(IApplicantView view)
        {
            _view = view;
        }

        public void OnViewLoaded()
        {
            StringPair[][] list = RecruitingGateway.GetRecruitingWorkflowListByApplicant(true);
            DataTable table = new DataTable();
            table.Locale = CultureInfo.CurrentCulture;

            TranslateListToDataTable(list, table);

            _view.ShowWorkflows(table);
        }

        private static void TranslateListToDataTable(StringPair[][] list, DataTable table)
        {
            if (list.Length > 0)
            {
                foreach (StringPair field in list[0])
                {
                    if (!string.Equals(field.Key, "WorkflowId"))
                    {
                        table.Columns.Add(field.Key);
                    }
                }
                foreach (StringPair[] record in list)
                {
                    DataRow row = table.NewRow();
                    List<StringPair> workflow = new List<StringPair>(record);
                    StringPair NextStep = StringPair.Get("NextStep", workflow);
                    StringPair WorkflowId = StringPair.Get("WorkflowId", workflow);
                    foreach (StringPair field in workflow)
                    {
                        if (!string.Equals(field.Key, "WorkflowId"))
                        {
                            row[field.Key] = field.Value;
                        }
                    }
                    table.Rows.Add(row);
                }
            }
        }
    }
}